$(function(){
	var dataTable = $("#bpCommentsDataTable").DataTable({
		"ajax": {
			"url": '/bpComments/list',
			"data": function(data){
				data.userId = $("#userId").val();
				data.postId = $("#postId").val();
				data.commentsId = $("#commentsId").val();
				data.content = $("#content").val();
				data.type = $("#type").val();
				data.status = $("#status").val();
				data.isClassic = $("#isClassic").val();
				data.isTop = $("#isTop").val();
			}
		},
		"ordering": false,
		"columns": [
//			{
//				"data": "id",
//				"className": "text-center sign-col",
//				"render": function(data, type, row, meta){
//					return '<input type="checkbox" name="pk" value="' + data + '" onclick="checkOne()"/>';
//				}
//			},
			{"data": "userId"},
			{
				"data": function(obj){
					var text = '';
					if (obj.type == '1') {
						text = '点赞';
					} else if (obj.type == '2') {
						text = '评论';
					}
					return text;
				}
			},
			{"data": "postId"},
			{"data": "commentsId"},
			{"data": "content"},
//			{"data": "isClassic"},
			{
				"data": function(obj){
					var text = '';
					if (obj.isClassic == '0') {
						text = '否';
					} else if (obj.isClassic == '1') {
						text = '是';
					}
					return text;
				}
			},
//			{"data": "isTop"},
			{
				"data": function(obj){
					var text = '';
					if (obj.isTop == '0') {
						text = '否';
					} else if (obj.isTop == '1') {
						text = '是';
					}
					return text;
				}
			},
			{
				"data": function(obj){
					return new Date(obj.releaseTime).Format("yyyy-MM-dd hh:mm:ss");
				} 
			},
//			{"data": "status"},
			{
				"data": function(obj){
					var text = '';
					if (obj.status == '0') {
						text = '屏蔽';
					} else if (obj.status == '1') {
						text = '正常';
					} else if (obj.status == '2') {
						text = '删除';
					}
					return text;
				}
			},
			{"data": "remark"},
			{
				"data": "id",
				"className": "operate-col",
				"data": function(data){
					var html = '<button type="button" onclick="view(\''+ data.id +'\')" class="btn btn-sm btn-info"><i class="fa fa-search"></i></button>';
					if(data.isClassic == '0' && data.status == '1'){
						html += '<button type="button" onclick="changeIsClassic(\''+ data.id +'\', \'1\')" class="btn btn-sm btn-info">加精</button>';
					} else if(data.isClassic == '1' && data.status == '1') {
						html += '<button type="button" onclick="changeIsClassic(\''+ data.id +'\', \'0\')" class="btn btn-sm btn-info">取消加精</button>';
					}
					if(data.isTop == '0' && data.status == '1'){
						html += '<button type="button" onclick="changeIsTop(\''+ data.id +'\', \'1\')" class="btn btn-sm btn-info">置顶</button>';
					} else if(data.isTop == '1' && data.status == '1'){
						html += '<button type="button" onclick="changeIsTop(\''+ data.id +'\', \'0\')" class="btn btn-sm btn-info">取消置顶</button>';
					}
					if(data.status == '1'){
						html += '<button type="button" onclick="changeStatus(\''+ data.id +'\', \'0\')" class="btn btn-sm btn-info">屏蔽</button>';
					}
					if(data.status == '0'){
						html += '<button type="button" onclick="deleteData(\''+ data.id +'\')" class="btn btn-sm btn-info">删除</button>';
					}
					return html;
				}
			}
		]
	});
	
	$('#searchBtn').click(function(){
		dataTable.ajax.reload();
	});
	
	$('#resetBtn').click(function(){
		$('#bpCommentsSearchForm')[0].reset();
		dataTable.ajax.reload();
	});
	
	$('#addBtn').click(function(){
		forwardPage('/bpComments/bpComments');
	});
	
	$('#delBtn').click(function(){
		ajaxBySelecteRows(dataTable,'/bpComments/delete','确定要删除？');
	});
	
	// 日期
	$('.datepicker').datetimepicker({
		language: 'zh-CN',
		format: 'yyyy-mm-dd',
		autoclose: true,
		todayBtn: true,
		clearBtn:true,
		minView: 2
	});
});

//function edit(id){
//	forwardPage('/bpComments/bpComments?id='+id);
//}

function view(id){
	forwardPage('/bpComments/view?id='+id);
}
function changeStatus(id, status){
	$.ajax({
		type: 'post',
		url: '/bpComments/changeStatus',
		data: {id: id, status: status},
		dataType: 'json',
		success: function(response){
			modalAlert(response.message);
			$("#bpCommentsDataTable").DataTable().ajax.reload();
		}
	});
}
function changeIsClassic(id, isClassic){
	$.ajax({
		type: 'post',
		url: '/bpComments/changeIsClassic',
		data: {id: id, isClassic: isClassic},
		dataType: 'json',
		success: function(response){
			modalAlert(response.message);
			$("#bpCommentsDataTable").DataTable().ajax.reload();
		}
	});
}
function changeIsTop(id, isTop){
	$.ajax({
		type: 'post',
		url: '/bpComments/changeIsTop',
		data: {id: id, isTop: isTop},
		dataType: 'json',
		success: function(response){
			modalAlert(response.message);
			$("#bpCommentsDataTable").DataTable().ajax.reload();
		}
	});
}
function deleteData(id){
	$.ajax({
		type: 'post',
		url: '/bpComments/delete',
		data: {id: id},
		dataType: 'json',
		success: function(response){
			modalAlert(response.message);
			$("#bpCommentsDataTable").DataTable().ajax.reload();
		}
	});
}